<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\AppUser;
use App\Services\Api\UserAppService;
use Illuminate\Http\Request;

class AppUserController extends Controller
{
    private $appUserService;

    public function __construct(UserAppService $appUserService)
    {
        $this->appUserService = $appUserService;
    }


    /**
     * 更新用户信息
     * @param Request $request
     * @return array
     */
    public function userInfo(Request $request)
    {
        try {

            if($request->isMethod('get')){
                $appUser = getAppUser();
                $data['name'] = $appUser->name;
                $data['sex'] = $appUser->gender;
                $data['id_card_no'] = $appUser->id_card_no;
                $data['birthday'] = $appUser->birthday;
                $data['area'] = $appUser->area;
                return $this->wrapSuccessReturn(compact('data'));
            }
            $validateRules = [
                'name'           => 'required',
                //'nick_name'      => 'sometimes',
                //'avatar'         => 'sometimes',//头像
                'sex'            => 'required',//性别:0未知,1男,2女
                'id_card_no'     => 'required',//身份证号码
                //'passport'       => 'sometimes',//护照
                'birthday'       => 'required',//生日
                'area'           => 'required',//地区
                'task'           => 'required',//任务
                //'id_card_a'      => 'sometimes',//身份证正面
                //'id_card_b'      => 'sometimes',//身份证反面
                //'country'        => 'sometimes',//国家
                //'city'           => 'sometimes',
                //'language'       => 'sometimes',
            ];

            $validatorMessages = [
                'name.required'      => "姓名不能为空",
                'sex.required'      => "性别不能为空",
                'id_card_no.required'      => "身份证号码不能为空",
                'birthday.required'      => "生日不能为空",
                'area.required'      => "地区不能为空",
            ];

            $this->requestValidator($request, $validateRules, $validatorMessages);
            $data = $this->appUserService->update($request);

            return $this->wrapSuccessReturn(compact('data'));
        } catch (\Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }
    }

    /**
     * 工作信息
     * @param Request $request
     * @return array
     */
    public function workerInfo(Request $request)
    {
        try {
            if($request->isMethod('get')){
                $appUser = getAppUser();
                $data['company'] = $appUser->company;
                $data['worker_address'] = $appUser->worker_address;
                $data['worker_time'] = $appUser->worker_time;
                $data['worker_level'] = $appUser->worker_level;
                $data['income'] = $appUser->income;
                return $this->wrapSuccessReturn(compact('data'));
            }
            $validateRules = [
                'company'        => 'required',//公司
                'worker_address' => 'required',//工作地址
                'worker_time'    => 'required',//入职时间
                'worker_level'   => 'required',//职级
                'income'         => 'required',//收入
                'task'           => 'required',//任务
            ];

            $validatorMessages = [
                'company.required'      => "公司名称不能为空",
                'worker_address.required'      => "工作地址不能为空",
                'worker_time.required'      => "工作年限不能为空",
                'worker_level.required'      => "职级不能为空",
                'income.required'      => "收入不能为空",
            ];

            $this->requestValidator($request, $validateRules, $validatorMessages);
            $data = $this->appUserService->updateWorkerInfo($request);

            return $this->wrapSuccessReturn(compact('data'));
        } catch (\Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }

    }

    /**
     * 车辆信息
     * @param Request $request
     * @return array
     */
    public function carInfo(Request $request)
    {
        try {
            if($request->isMethod('get')){
                $appUser = getAppUser();
                $data['car_a'] = $appUser->car_b;
                $data['car_b'] = $appUser->car_b;
                return $this->wrapSuccessReturn(compact('data'));
            }
            $validateRules = [
                'car_a'          => 'required',//车辆行驶证a
                'car_b'          => 'required',//车辆行驶证b
                'task'           => 'required',//任务
            ];

            $validatorMessages = [
                'car_a.required'      => "行驶证正本不能为空",
                'car_b.required'      => "行驶证副本不能为空",
            ];

            $this->requestValidator($request, $validateRules, $validatorMessages);
            $data = $this->appUserService->updateCarInfo($request);

            return $this->wrapSuccessReturn(compact('data'));
        } catch (\Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }

    }

    /**
     * 房产信息
     * @param Request $request
     * @return array
     */
    public function houseInfo(Request $request)
    {
        try {
            if($request->isMethod('get')){
                $appUser = getAppUser();
                $data['house'] = $appUser->house;
                return $this->wrapSuccessReturn(compact('data'));
            }
            $validateRules = [
                'house'          => 'require',//房产信息
                'task'           => 'required',//任务
            ];

            $validatorMessages = [
                'house.require' => "房产信息不能空"
            ];

            $this->requestValidator($request, $validateRules, $validatorMessages);
            $data = $this->appUserService->updateHouseInfo($request);

            return $this->wrapSuccessReturn(compact('data'));
        } catch (\Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }

    }

    /**
     * 学校信息
     * @param Request $request
     * @return array
     */
    public function schoolInfo(Request $request)
    {
        try {
            if($request->isMethod('get')){
                $appUser = getAppUser();
                $data['school'] = $appUser->school;
                return $this->wrapSuccessReturn(compact('data'));
            }
            $validateRules = [
                'school'         => 'require',//学校
            ];

            $validatorMessages = [
                'school.require' => '学校信息不能为空'
            ];

            $this->requestValidator($request, $validateRules, $validatorMessages);
            $data = $this->appUserService->updateSchoolInfo($request);

            return $this->wrapSuccessReturn(compact('data'));
        } catch (\Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }

    }
}
